package cn.edu.cxcyzx.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.cxcyzx.algorithm.CalMd5;
import cn.edu.cxcyzx.entity.Teacher;
import cn.edu.cxcyzx.entity.User;
import cn.edu.cxcyzx.service.TeacherService;
import cn.edu.cxcyzx.supervisor.DaoFit;
import cn.edu.cxcyzx.supervisor.ResultDo;
import cn.edu.cxcyzx.util.PropertyPlaceholder;

@Service("TeacherService")
public class TeacherServiceImpl implements TeacherService {
	
	@Resource
	private DaoFit daoFit;
	
	@Override
	public ResultDo<Teacher> selectByTeaName(String teaName) {
		// TODO Auto-generated method stub
		ResultDo<Teacher> resultDo=new ResultDo<Teacher>();
		if(teaName!=null&&"".equals(teaName.trim())){
			Teacher tea=daoFit.getTeacherDao().selectByTeaName(teaName);
			if(tea!=null){
				resultDo.setResult(tea);
				resultDo.setSuccess(true);
			}else{
				resultDo.setSuccess(false);
			}
		}
		return resultDo;
	}

	@Override
	public ResultDo<List<Teacher>> getAllTea(String teaName) {
		// TODO Auto-generated method stub
		ResultDo<List<Teacher>> resultDo=new ResultDo<List<Teacher>>();
		List<Teacher> list=null;
		if(teaName.equals("")){
			list=daoFit.getTeacherDao().getAllTea();
		}else{
			/**
			 * 模糊查询教师
			 */
			list=daoFit.getTeacherDao().selectLikeByTeaName(teaName);
		}
		System.out.println("========:"+list.size());
		resultDo.setResult(list);
		resultDo.setSuccess(true);
		return resultDo;
	}

	@Override
	public void insertTea(Teacher teacher) {
		// TODO Auto-generated method stub
		System.out.println(teacher.getTeaEmail());
		if(teacher!=null)
		{
			daoFit.getTeacherDao().insertTea(teacher);
			
			String secret=CalMd5.MD5((String)PropertyPlaceholder.getProperty("DEFAULT_PASSWORD"),teacher.getTeaId());
			User u=new User(teacher.getTeaId(),secret);
			u.setRole(3);
			daoFit.getUserDao().insertUser(u);
		}
		
	}

	@Override
	public void deleteTeaByTeaId(String teaId) {
		// TODO Auto-generated method stub
		if(teaId!=null){
			daoFit.getTeacherDao().deleteTeaByTeaId(teaId);
		}
	}

	@Override
	public void updateTea(Teacher tea) {
		// TODO Auto-generated method stub
		if(tea!=null){
			daoFit.getTeacherDao().updateTea(tea);
		}
	}

}
